﻿
    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class goodspay : Page
    {

        private Chain.BLL.Mem MemBll = new Chain.BLL.Mem();

        private Chain.BLL.MicroWebsiteGoods MicroWebsiteGoodsBll = new Chain.BLL.MicroWebsiteGoods();
        private Chain.BLL.MicroWebsiteOrderLog MicroWebsiteOrderLogBll = new Chain.BLL.MicroWebsiteOrderLog();
        private Chain.BLL.MicroWebsiteOrderLogDetail MicroWebsiteOrderLogDetailBll = new Chain.BLL.MicroWebsiteOrderLogDetail();

        private decimal TotalSubtotal;


        private void BindGoodsinventory(int MicroOrderID)
        {
            DataTable dt = this.MicroWebsiteOrderLogDetailBll.GetList("MicroOrderID=" + MicroOrderID).Tables[0];
            StringBuilder sbWhere = new StringBuilder("MicroGoodsID in(");
            StringBuilder sbKeys = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbWhere.AppendFormat("{0},", dt.Rows[i]["MicroGoodsID"]);
                sbKeys.AppendFormat("{0},{1}|", dt.Rows[i]["MicroGoodsID"], dt.Rows[i]["MicroOrderDetailNumber"]);
            }
            sbWhere.Append("0)");
            DataTable dtGoods = this.MicroWebsiteGoodsBll.GetList(sbWhere.ToString()).Tables[0];
            string[] keys = sbKeys.ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            dtGoods = this.UpDateTable(dtGoods, keys);
            this.rptGoodsinventory.DataSource = dtGoods;
            this.rptGoodsinventory.DataBind();
            this.total.InnerText = "应付总金额：￥" + this.TotalSubtotal.ToString("0.00");
        }

        private void BindPayInfo(int MemID, int MicroOrderID)
        {
            Chain.Model.Mem MemModel = this.MemBll.GetModel(MemID);
            this.MemMoney.InnerText = MemModel.MemMoney.ToString("0.00");
            Chain.Model.MicroWebsiteOrderLog MicroWebsiteOrderLogModel = this.MicroWebsiteOrderLogBll.GetModel(MicroOrderID);
            this.payCard.Value = this.payMoney.InnerText = MicroWebsiteOrderLogModel.MicroOrderDiscountMoney.ToString("0.00");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int MicroOrderID = Convert.ToInt32(base.Request["MicroOrderID"]);
            if ((base.Request.Cookies["uid"] != null) && Regex.IsMatch(base.Request.Cookies["uid"].Value, @"^\d+$"))
            {
                int MemID = Convert.ToInt32(base.Request.Cookies["uid"].Value);
                this.BindPayInfo(MemID, MicroOrderID);
                this.BindGoodsinventory(MicroOrderID);
                this.txtMicroOrderID.Value = MicroOrderID.ToString();
                string type = base.Request["type"];
                if (!string.IsNullOrEmpty(type) && (type == "lookinventory"))
                {
                    this.goodspayinfo.Style.Add("display", "none");
                    this.total.InnerText = "总金额：￥" + this.TotalSubtotal.ToString("0.00");
                    this.card.Attributes.Add("class", "ui-btn-active");
                }
                else
                {
                    this.buy.Attributes.Add("class", "ui-btn-active");
                }
            }
        }

        private DataTable UpDateTable(DataTable dt, string[] keys)
        {
            dt.Columns.Add("ConvertNumber");
            dt.Columns.Add("Subtotal");
            foreach (string item in keys)
            {
                int MicroGoodsID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
                int ConvertNumber = Convert.ToInt32(item.Split(new char[] { ',' })[1]);
                DataRow row = dt.Select("MicroGoodsID=" + MicroGoodsID)[0];
                row["ConvertNumber"] = ConvertNumber;
                row["Subtotal"] = ConvertNumber * Convert.ToInt32(row["MicroPrice"]);
                this.TotalSubtotal += Convert.ToInt32(row["Subtotal"]);
            }
            return dt;
        }
    }


